package com.tgpms.web.assets.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.util.PageView;
import com.tgpms.util.VformBeanUtil;
import com.tgpms.web.assets.dao.FaCheckItemsMapper;
import com.tgpms.web.assets.dao.FaCheckMapper;
import com.tgpms.web.assets.entity.FaCheck;
import com.tgpms.web.assets.entity.FaCheckItems;
import com.tgpms.web.assets.entity.FaCheckItemsKey;
import com.tgpms.web.assets.service.FaCheckService;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class FaCheckServiceImpl implements FaCheckService {

	@Autowired
	private VformInterface vformInterface;
	
	/**
	* @Author newway
	* @Description 查询固定资产盘点信息
	* @Date 2019年8月2日上午9:25:44
	* @Param com.tgpms.util.PageView
	* @return PageView
	**/
	@Override
	public Result selFaCheckList(PageInfo pageInfo) {
		JSONObject param = new JSONObject();
		Object selparam = pageInfo.getQueryMap().get("selparam");
		Object leftdition = pageInfo.getQueryMap().get("leftdition");
		Object condition = pageInfo.getQueryMap().get("condition");
		Object dataowner = pageInfo.getQueryMap().get("dataowner");
		if(Common.isEmpty(selparam)) {
			param.put("[SQL1]", " ");
		}else {
			param.put("[SQL1]", selparam.toString());
		}
		if(Common.isEmpty(leftdition)) {
			param.put("[SQL2]", " ");
		}else {
			param.put("[SQL2]", leftdition.toString());
		}
		if(Common.isEmpty(condition)) {
			param.put("[SQL3]", " ");
		}else {
			param.put("[SQL3]", condition.toString());
		}
		Result result = vformInterface.multiForMsQueryBySql("SCWZC015", param.toJSONString(), pageInfo.getPage(), 
				pageInfo.getSize(), "Y", "Y", dataowner.toString());
		return result;
	}
	

	/**
	* @Author newway
	* @Description 添加固定资产盘点信息
	* @Date 2019年8月1日下午4:48:38
	* @Param com.tgpms.web.assets.entity.FaCheck
	* @return Integer
	**/
	@SuppressWarnings("unchecked")
	@Override
	public Map<String,String> addFaCheckInfo(FaCheck faCheck) {
		Map<String,String> map = new HashMap<String, String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String mon = sdf.format(faCheck.getCheckDate());
		String chno = mon+faCheck.getManageUnit()+faCheck.getCheckDept();
		faCheck.setCheckNo(chno);
		//对象转json字符串, 字段驼峰转下划线大写字段
		JSONObject json = VformBeanUtil.entityToJsonString(faCheck);
		if(json==null || json.isEmpty()) {
			map.put("code", "-2");
			map.put("info", ExceptionExplain.ERROR_BY_PARSING.getExplain());
			return map;
		}
		System.out.println("vform增加上送参数   "+json.toString());
		//vform同步插入
		Result result = vformInterface.insert("D8006", json);
		System.out.println("vform增加返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		if(resData.equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			map.put("data", faCheck.getCheckNo());
		}
		return map;
	}

	/**
	* @Author newway
	* @Description 更新固定资产盘点信息
	* @Date 2019年8月1日下午5:31:59
	* @Param com.tgpms.web.assets.entity.FaCheck
	* @return Integer
	**/
	@Override
	public Map<String,String> updateFaCheckInfo(FaCheck faCheck) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("CHECK_NO", faCheck.getCheckNo());
		JSONObject pajson = new JSONObject();
		pajson = VformBeanUtil.entityToJsonStringByUpdate(faCheck);
		pajson.remove("CHECK_NO");

		System.out.println("vform更新上送主键   "+keyjson.toString());
		System.out.println("vform更新上送参数   "+pajson.toString());
		//vform同步更新
		Result result = vformInterface.update("D8006", keyjson.toString(),pajson.toString(),"Y","Y",faCheck.getDataOwner());
		System.out.println("vform更新返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}
		return map;
	}

	/**
	* @Author newway
	* @Description 删除固定资产盘点信息
	* @Date 2019年8月1日下午5:49:16
	* @Param String
	* @return Integer
	**/
	@Override
	public Map<String,String> deleteFaCheckInfo(String checkNo) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("CHECK_NO", checkNo);
		System.out.println("vform删除上送主键   "+keyjson.toString());
		//vform同步删除
		Result result = vformInterface.delete("D8006", keyjson.toJSONString());
		System.out.println("vform删除返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}
		return map;
	}
	
	
	/**
	* @Author newway
	* @Description 查询固定资产盘点维护信息
	* @Date 2019年8月19日下午2:49:13
	* @Param com.tgpms.web.assets.entity.FaCheckItems
	* @return Map<String,String>
	**/
	@Override
	public Result selFaCheckItemsList(PageInfo pageInfo) {
		JSONObject param = new JSONObject();
		Object selparam = pageInfo.getQueryMap().get("selparam");
		Object leftdition = pageInfo.getQueryMap().get("leftdition");
		Object condition = pageInfo.getQueryMap().get("condition");
		Object dataowner = pageInfo.getQueryMap().get("dataowner");
		if(Common.isEmpty(selparam)) {
			param.put("[SQL1]", " ");
		}else {
			param.put("[SQL1]", selparam.toString());
		}
		if(Common.isEmpty(leftdition)) {
			param.put("[SQL2]", " ");
		}else {
			param.put("[SQL2]", leftdition.toString());
		}
		if(Common.isEmpty(condition)) {
			param.put("[SQL3]", " ");
		}else {
			param.put("[SQL3]", condition.toString());
		}
		Result result = vformInterface.multiForMsQueryBySql("SCWZC016", param.toJSONString(), pageInfo.getPage(), 
				pageInfo.getSize(), "Y", "Y" +
						"", dataowner.toString());
		return result;
	}

	/**
	* @Author newway
	* @Description 增加固定资产盘点维护信息
	* @Date 2019年8月19日下午2:49:13
	* @Param com.tgpms.web.assets.entity.FaCheckItems
	* @return Integer
	**/
	@Override
	public Map<String,String> addFaCheckItemsInfo(FaCheckItems faCheckItems) {
		Map<String,String> map = new HashMap<String, String>();
		FaCheckItemsKey key = new FaCheckItemsKey();
		key.setFixedAssetNo(faCheckItems.getFixedAssetNo());
		key.setCheckNo(faCheckItems.getCheckNo());
		//对象转json字符串, 字段驼峰转下划线大写字段
		JSONObject json = VformBeanUtil.entityToJsonString(faCheckItems);
		if(json==null || json.isEmpty()) {
			map.put("code", "-2");
			map.put("info", ExceptionExplain.ERROR_BY_PARSING.getExplain());
			return map;
		}
		json.put("CHECK_NO", key.getCheckNo());
		json.put("FIXED_ASSET_NO", key.getFixedAssetNo());
		System.out.println("vform增加上送参数   "+json.toString());
		//vform同步插入
		Result result = vformInterface.insert("D8014", json);
		System.out.println("vform增加返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		if(resData.equals("{\"message\":\"login\"}")) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_OVERDUE.getExplain());
			return map;
		}
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
		}
		return map;
	}

	/**
	* @Author newway
	* @Description 更新固定资产盘点维护信息
	* @Date 2019年8月19日下午2:49:13
	* @Param com.tgpms.web.assets.entity.FaCheckItems
	* @return Map<String,String>
	**/
	@Override
	public Map<String, String> updateFaCheckItemsInfo(FaCheckItems faCheckItems) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("CHECK_NO", faCheckItems.getCheckNo());
		keyjson.put("FIXED_ASSET_NO", faCheckItems.getFixedAssetNo());
		JSONObject pajson = new JSONObject();
		pajson = VformBeanUtil.entityToJsonStringByUpdate(faCheckItems);
		pajson.remove("CHECK_NO");
		pajson.remove("FIXED_ASSET_NO");

		System.out.println("vform更新上送主键   "+keyjson.toString());
		System.out.println("vform更新上送参数   "+pajson.toString());
		//vform同步更新
		Result result = vformInterface.update("D8014", keyjson.toString(),pajson.toString(),null,null,null);
		System.out.println("vform更新返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}
		return map;
	}

	/**
	* @Author newway
	* @Description 删除固定资产盘点维护信息
	* @Date 2019年8月19日下午2:49:13
	* @Param fixedAssetNo
	* @Param checkNo
	* @return Map<String,String>
	**/
	@Override
	public Map<String, String> deleteFaCheckItemsInfo(String fixedAssetNo, String checkNo) {
		Map<String,String> map = new HashMap<String, String>();
		JSONObject keyjson = new JSONObject();
		keyjson.put("CHECK_NO", checkNo);
		keyjson.put("FIXED_ASSET_NO", fixedAssetNo);
		System.out.println("vform删除上送主键   "+keyjson.toString());
		//vform同步删除
		Result result = vformInterface.delete("D8014", keyjson.toJSONString());
		System.out.println("vform删除返回结果   "+result);
		if(result==null || !result.getSuccess()) {
			map.put("code", "-3");
			map.put("info", ExceptionExplain.ERROR_BY_VFORM_POST.getExplain());
			return map;
		}
		String resData = result.getData().toString();
		JSONObject reJson = JSONObject.parseObject(resData);
		if(reJson.get("status").toString().equals("error")) {
			map.put("code", "-4");
			map.put("info", reJson.get("validateError").toString());
			return map;
		}else {
			map.put("code", "1");
			map.put("info", ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}
		return map;
	}

	

	
	

}
